package cn.datawin.bms.modules.deploy.service.impl;

import cn.datawin.bms.deploy.entity.DeplModuleInfoEntity;
import cn.datawin.bms.deploy.entity.DeplServerModuleInfoEntity;
import cn.datawin.bms.deploy.vo.ServerModuleInfoVo;
import cn.datawin.bms.modules.deploy.mapper.DeplServerModuleInfoMapper;
import cn.datawin.bms.modules.deploy.service.DeplModuleInfoService;
import cn.datawin.bms.modules.deploy.service.DeplServerModuleInfoService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 服务器端口信息表
 *
 * @author shengdongcloud
 * @date 2025-03-13 13:55:38
 */
@RequiredArgsConstructor
@Service
public class DeplServerModuleInfoServiceImpl extends ServiceImpl<DeplServerModuleInfoMapper, DeplServerModuleInfoEntity> implements DeplServerModuleInfoService {

	private final DeplModuleInfoService deplModuleInfoService;


	@Override
	public Page<ServerModuleInfoVo> customPage(Page page, DeplServerModuleInfoEntity serverModuleInfo) {


		//1、分页获取数据
		Page result = this.page(page);

		//2、填充模块名称
		List<DeplServerModuleInfoEntity> records = result.getRecords();
		List<ServerModuleInfoVo> voList = new ArrayList<>();
		if(CollUtil.isNotEmpty(records)) {
			List<Integer> collect = records.stream().map(DeplServerModuleInfoEntity::getModuleId).collect(Collectors.toList());
			List<DeplModuleInfoEntity> moduleInfoEntities = deplModuleInfoService.listByIds(collect);
			Map<Integer, String> collect1 = moduleInfoEntities.stream().collect(Collectors.toMap(DeplModuleInfoEntity::getId, DeplModuleInfoEntity::getName));
			for (DeplServerModuleInfoEntity record : records) {
				ServerModuleInfoVo serverModuleInfoVo = new ServerModuleInfoVo();
				BeanUtil.copyProperties(record,serverModuleInfoVo);
				serverModuleInfoVo.setId(record.getId());
				serverModuleInfoVo.setModuleName(collect1.get(record.getModuleId()));
				voList.add(serverModuleInfoVo);
			}
		}
		//3、返回结果
		Page<ServerModuleInfoVo> extendedPage = new Page<>();
		extendedPage.setCurrent(result.getCurrent());
		extendedPage.setSize(result.getSize());
		extendedPage.setTotal(result.getTotal());
		extendedPage.setPages(result.getPages());
		extendedPage.setRecords(voList);

		return extendedPage;
	}
}
